package com.example.cet4try.myDialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cet4try.R;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.Objects;
//https://github.com/hanzhanbing/AVLoadingIndicatorView

public class LoadDialog extends Dialog {

    private String text1;
    private Context mContext;
    private String indicator = "BallClipRotateMultipleIndicator";
    private boolean cancelflag = false;
    //private Activity mactivity = null;

    public LoadDialog(Context context) {
        super(context, R.style.MyMessageDialog);
        this.mContext = context;
    }

    public void setTitle(String text1){
        this.text1 = text1;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.load_dialog);
        iniView();
        setCancelable(false);
    }

    @SuppressLint("SetTextI18n")
    private void iniView() {
        TextView textView1 = findViewById(R.id.text1);
        textView1.setText(text1+"...");
        AVLoadingIndicatorView load = findViewById(R.id.load);
        load.setIndicator(indicator);
        load.show();

    }

    public void setIndicator(String indicator) {
        this.indicator = indicator;
    }

    public void setCancel(boolean cancelflag) {
        this.cancelflag = cancelflag;
    }

    private long exitTime = 0;
    @Override
    public void onBackPressed(){
        if (cancelflag){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(mContext.getApplicationContext(), "再按一次取消操作", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                dismiss();
                Activity activity = (Activity) mContext;
                activity.finish();
                //Objects.requireNonNull(getOwnerActivity()).finish();
            }
        }
        else
            Toast.makeText(mContext,text1+"，请稍后",Toast.LENGTH_SHORT).show();
    }



}
